<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MallBrokerageWithdrawApi } from '#/api/mall/trade/brokerage/withdraw';

import { h } from 'vue';

import { confirm, Page, prompt } from '@vben/common-ui';
import {
  BrokerageWithdrawStatusEnum,
  BrokerageWithdrawTypeEnum,
  DICT_TYPE,
} from '@vben/constants';
import { formatDateTime } from '@vben/utils';

import { Input, message } from 'ant-design-vue';

import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  approveBrokerageWithdraw,
  getBrokerageWithdrawPage,
  rejectBrokerageWithdraw,
} from '#/api/mall/trade/brokerage/withdraw';
import { DictTag } from '#/components/dict-tag';
import { $t } from '#/locales';

import { useGridColumns, useGridFormSchema } from './data';

/** 分销佣金提现 */
defineOptions({ name: 'BrokerageWithdraw' });

/** 刷新表格 */
function handleRefresh() {
  gridApi.query();
}

/** 审核通过 */
async function handleApprove(row: MallBrokerageWithdrawApi.BrokerageWithdraw) {
  await confirm($t('views.mall.trade.brokerage.withdraw.approveConfirmMessage'));
  const hideLoading = message.loading({
    content: $t('views.mall.trade.brokerage.withdraw.approveLoadingMessage'),
    duration: 0,
  });
  try {
    await approveBrokerageWithdraw(row.id);
    message.success($t('ui.actionMessage.operationSuccess'));
    handleRefresh();
  } finally {
    hideLoading();
  }
}

/** 审核驳回 */
function handleReject(row: MallBrokerageWithdrawApi.BrokerageWithdraw) {
  prompt({
    component: () => {
      return h(Input, {
        placeholder: $t('views.mall.trade.brokerage.withdraw.rejectReasonPlaceholder'),
        allowClear: true,
        rules: [{ required: true, message: $t('views.mall.trade.brokerage.withdraw.rejectReasonRequired') }],
      });
    },
    content: $t('views.mall.trade.brokerage.withdraw.rejectContent'),
    title: $t('views.mall.trade.brokerage.withdraw.rejectTitle'),
    modelPropName: 'value',
  }).then(async (val) => {
    if (val) {
      await rejectBrokerageWithdraw({
        id: row.id!,
        auditReason: val,
      });
      handleRefresh();
    }
  });
}

/** 重新转账 */
async function handleRetryTransfer(
  row: MallBrokerageWithdrawApi.BrokerageWithdraw,
) {
  await confirm($t('views.mall.trade.brokerage.withdraw.retryTransferConfirmMessage'));
  const hideLoading = message.loading({
    content: $t('views.mall.trade.brokerage.withdraw.approveLoadingMessage'),
    duration: 0,
  });
  try {
    await approveBrokerageWithdraw(row.id);
    message.success($t('ui.actionMessage.operationSuccess'));
    handleRefresh();
  } finally {
    hideLoading();
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useGridColumns(),
    height: 'auto',
    keepSource: true,
    cellConfig: {
      height: 90,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getBrokerageWithdrawPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<MallBrokerageWithdrawApi.BrokerageWithdraw>,
});
</script>

<template>
  <Page auto-content-height>
    <Grid :table-title="$t('views.mall.trade.brokerage.withdraw.list')">
      <template #withdraw-info="{ row }">
        <div
          v-if="row.type === BrokerageWithdrawTypeEnum.WALLET.type"
          class="text-left"
        >
          -
        </div>
        <div v-else class="text-left">
          <div v-if="row.userAccount" class="text-left">
            {{ `${$t('views.mall.trade.brokerage.withdraw.account')}: ${row.userAccount}` }}
          </div>
          <div v-if="row.userName" class="text-left">
            {{ `${$t('views.mall.trade.brokerage.withdraw.username')}: ${row.userName}` }}
          </div>
          <template v-if="row.type === BrokerageWithdrawTypeEnum.BANK.type">
            <div v-if="row.bankName" class="text-left">
              {{ `${$t('views.mall.trade.brokerage.withdraw.bankName')}: ${row.bankName}` }}
            </div>
            <div v-if="row.bankAddress" class="text-left">
              {{ `${$t('views.mall.trade.brokerage.withdraw.bankAddress')}: ${row.bankAddress}` }}
            </div>
          </template>
          <div v-if="row.qrCodeUrl" class="mt-2 text-left">
            <div class="flex items-start gap-2">
              <span class="flex-shrink-0">{{ `${$t('views.mall.trade.brokerage.withdraw.qrCode')}:` }}</span>
              <img :src="row.qrCodeUrl" class="h-10 w-10 flex-shrink-0" />
            </div>
          </div>
        </div>
      </template>
      <template #status-info="{ row }">
        <div>
          <DictTag
            :value="row.status"
            :type="DICT_TYPE.BROKERAGE_WITHDRAW_STATUS"
          />
          <div
            v-if="row.auditTime"
            class="mt-1 text-left text-xs text-gray-500"
          >
            {{ `${$t('views.mall.trade.brokerage.withdraw.auditTime')}: ${formatDateTime(row.auditTime)}` }}
          </div>
          <div
            v-if="row.auditReason"
            class="mt-1 text-left text-xs text-gray-500"
          >
            {{ `${$t('views.mall.trade.brokerage.withdraw.auditTime')}: ${row.auditReason}` }}
          </div>
          <div
            v-if="row.transferErrorMsg"
            class="mt-1 text-left text-xs text-red-500"
          >
            {{ `${$t('views.mall.trade.brokerage.withdraw.transferErrorMsg')}: ${row.transferErrorMsg}` }}
          </div>
        </div>
      </template>

      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: $t('views.mall.trade.brokerage.withdraw.approve'),
              type: 'link',
              icon: ACTION_ICON.EDIT,
              auth: ['trade:brokerage-withdraw:audit'],
              ifShow:
                row.status === BrokerageWithdrawStatusEnum.AUDITING.status &&
                !row.payTransferId,
              onClick: () => handleApprove(row),
            },
            {
              label: $t('views.mall.trade.brokerage.withdraw.reject'),
              type: 'link',
              danger: true,
              icon: ACTION_ICON.DELETE,
              auth: ['trade:brokerage-withdraw:audit'],
              ifShow:
                row.status === BrokerageWithdrawStatusEnum.AUDITING.status &&
                !row.payTransferId,
              onClick: () => handleReject(row),
            },
            {
              label: $t('views.mall.trade.brokerage.withdraw.retryTransfer'),
              type: 'link',
              icon: ACTION_ICON.REFRESH,
              auth: ['trade:brokerage-withdraw:audit'],
              ifShow:
                row.status === BrokerageWithdrawStatusEnum.WITHDRAW_FAIL.status,
              onClick: () => handleRetryTransfer(row),
            },
          ]"
        />
      </template>
    </Grid>
  </Page>
</template>
